var module=angular.module("userApp",[]);
module.controller("userController",function($scope,$http){	
	
	//初始化请求
	$scope.initQuery = function(){
		$scope.size = 10;
		$scope.nowPage = 1;
		$scope.queryUser();
	}
	
	//确认键绑定事件
	$("#qStringInput").bind("keyup", function(event) {
		var e = event;
		if(e && e.keyCode == 13){
			$scope.initQuery();
		}
	});
	//请求
	$scope.queryUser = function (){
		if($scope.nowPage==null)$scope.nowPage=$scope.current;
		$http({
			method:'post',
			url:context+'/user/ajax',
			data:{
				'qString':$scope.qString,
				'size':$scope.size,
				'page':$scope.nowPage
				}
				
		}).success(function(data, status, headers, config){
			$scope.userList = data.userList;
			//刷新分页参数
			$scope.refreshPage(data.page,data.count,data.size);
		
		});
	}
	

	$scope.upUser = function(id){
		if($scope.nowPage==null)$scope.nowPage=$scope.current;
		$http({
			method:'post',
			url:context+'/user/upUserAjax',
			data:{
				'userId':id,
				'qString':$scope.qString,
				'size':$scope.size,
				'page':$scope.nowPage
			}
		}).success(function(data, status, headers, config){
			$scope.userList = data.userList;
			//刷新分页参数
			$scope.refreshPage(data.page,data.count,data.size);
			
		});
	}
	$scope.deUser = function(id){
		if($scope.nowPage==null)$scope.nowPage=$scope.current;
		$http({
			method:'post',
			url:context+'/user/deUserAjax',
			data:{
				'userId':id,
				'qString':$scope.qString,
				'size':$scope.size,
				'page':$scope.nowPage
			}
		}).success(function(data, status, headers, config){
			$scope.userList = data.userList;
			//刷新分页参数
			$scope.refreshPage(data.page,data.count,data.size);
			
		});
	}
	
	
	//刷新分页
	$scope.refreshPage = function(current,toalNum,size){//
		//已知参数
		$scope.maxPage=8;
		$scope.current = current;
		$scope.toalNum = toalNum;
		$scope.size = size;
	
		$scope.totalPage = Math.ceil($scope.toalNum/$scope.size)//总页数
		$scope.pre=($scope.current-1<1)?1:$scope.current-1;//上一页页码
		$scope.next=($scope.current+1>$scope.totalPage)?$scope.totalPage:$scope.current+1;//下一页页码
		$scope._currentPage = Math.ceil($scope.current/$scope.maxPage);//当前页所在分页的页码
		$scope._startPage = ($scope._currentPage - 1)*$scope.maxPage + 1;//每页的开始页面
		var _maxPage = Math.ceil($scope.totalPage/$scope.maxPage);//最大页数
		$scope._firstPage = $scope._startPage;
		$scope.maxPageArr = [];
		for(var i=0; i<$scope.maxPage;i++){
			if($scope._startPage<=$scope.totalPage){
				$scope.maxPageArr[i] = $scope._startPage;
				$scope._startPage++;
			}else{
				break;
			}
		}
		$scope._endPage = $scope._startPage;
	}//end
	$scope.refreshPage($scope.page,$scope.count,$scope.size);
	
	//翻页
	$scope.page = function (p){
		$scope.nowPage = p;
		$scope.queryUser();
	}
	
	//分页中的 ...按钮，重置页码
	$scope.resetPage = function(v){
		if (v=="pre") {
			$scope._currentPage = $scope._currentPage-1;
		}else if(v=="next"){
			$scope._currentPage = $scope._currentPage+1;
		}
		$scope._startPage = ($scope._currentPage-1)*$scope.maxPage+1;//开始
		$scope._firstPage = $scope._startPage;
		$scope.maxPageArr=[];
		for(var i=0;i<$scope.maxPage;i++){
			if($scope._startPage<=$scope.totalPage){
				$scope.maxPageArr[i] = $scope._startPage;
				$scope._startPage++;
			}else{
				 break;
			}
		}
	}
	
	
	$scope.isDelete = function(user){
		 $.confirm({
			    title: '确认删除'+user.name,
			    content: false,
			    confirm: function(){
			    	$scope.deUser(user.userId);
//			    	location.href=context+"/user/deleteUser?userId="+user.userId;
			    },
			    confirmButton:'确认',		
			    cancelButton:'取消',			
			    theme: 'white'	//		
			});

	   
	}	
	
});


//时间格式过滤
module.filter('userDate',function(){
    return function(date){
       var str = date.substring(0,date.lastIndexOf('.'));
       return str;
    }
});


